﻿namespace FFF.Test.Application.DomainServices.FW
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Abp.Domain.Services;
    using Abp.Domain.Uow;
    using Abp.EntityFrameworkCore.Uow;
    using Abp.EntityFrameworkCore;
    using AutoMapper;
    using Abp.UI;
    using Ydm.Helper;
    using Ydm.Extensions;
    using Microsoft.EntityFrameworkCore;
    using AutoMapper.QueryableExtensions;
    using FFF.Test.DataAccess.Framework;
    using FFF.Test.Application.Repositories.FW;
    using FFF.Test.Entity.FW;
    using FFF.Test.Entity.FW.Dto;
    using FFF.Test.Entity.FW.View;

    /// <summary>
    /// 用户模块配置表领域层服务接口
    /// </summary>
    public class FWUserFunctionSetDomainService : YdmAbpDomainServiceBase, IFWUserFunctionSetDomainService
    {
        /// <summary>
        /// 用户模块配置表仓储
        /// </summary>
        private readonly IFWUserFunctionSetRepository _fWUserFunctionSetRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="FWUserFunctionSetDomainService"/> class.
        /// 用户模块配置表领域层操作
        /// </summary>
        /// <param name="fWUserFunctionSetRepository">用户模块配置表仓储</param>
        /// <param name="dbContextProvider">数据库访问对象</param>
        public FWUserFunctionSetDomainService(
            IFWUserFunctionSetRepository fWUserFunctionSetRepository
        )
        {
            this._fWUserFunctionSetRepository = fWUserFunctionSetRepository;
        }

        /// <summary>
        /// 1.006 批量保存模块排序(于大明)
        /// </summary>
        /// <param name="inDto">批量保存模块排序输入dto</param>
        /// <returns>批量保存模块排序输出dto</returns>
        public async Task<BatchSaveOrderRuleOutDto> BatchSaveOrderRuleAsync(BatchSaveOrderRuleInDto inDto)
        {
            var result = new BatchSaveOrderRuleOutDto();
            foreach (var dto in inDto.Dtos)
            {
                var entity = await this._fWUserFunctionSetRepository.GetAsync(dto.Id);
                entity.UserFunctionSetSort = dto.UserFunctionSetSort;
                await this._fWUserFunctionSetRepository.UpdateAsync(entity);
            }

            return result;
        }

        /// <summary>
        /// 1.005 批量保存模块信息(于大明)
        /// </summary>
        /// <param name="inDto">批量保存模块信息输入dto</param>
        /// <returns>批量保存模块信息输出dto</returns>
        public async Task<BatchSaveFunctionOutDto> BatchSaveFunctionAsync(BatchSaveFunctionInDto inDto)
        {
            var userFunctionSet = this._fWUserFunctionSetRepository.GetView();

            // 找到目前的用户的功能GUIDs
            List<Guid> currentFunctionGUIDs = await userFunctionSet
                .Where(x => x.UserGUID == inDto.UserGUID && x.FunctionGUID != null)
                .Select(x => x.FunctionGUID ?? Guid.Empty).ToListAsync();

            // 找出要删除的functionsguid
            IEnumerable<Guid> deleteGUIDs = currentFunctionGUIDs.Except(inDto.FunctionGUIDs);

            // 删除
            await this._fWUserFunctionSetRepository.DeleteAsync(x => x.UserGUID == inDto.UserGUID && deleteGUIDs.Contains(x.FunctionGUID ?? Guid.Empty));

            // 找出新增的guid
            IEnumerable<Guid> addGUIDs = inDto.FunctionGUIDs.Except(currentFunctionGUIDs);

            // 新增
            IEnumerable<FWUserFunctionSetCreateInputDto> addDtos = addGUIDs.Select(x => new FWUserFunctionSetCreateInputDto
            {
                FunctionGUID = x,
                IsSiteInner = 1,
                UserGUID = inDto.UserGUID,
                UserFunctionSetSort = 9999
            });

            foreach (var dto in addDtos)
            {
                await this._fWUserFunctionSetRepository.CreateByDtoAsync(dto);
            }

            BatchSaveFunctionOutDto result = new BatchSaveFunctionOutDto();
            return result;
        }
    }
}
